package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author : [chenys]
 * @version : [v1.0]
 * @Description : 预约接口实现
 * @createTime : [2022/5/24 15:08]
 */
@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;

    /**
     * 新增,批量添加
     * @author chenxys
     * @param orderSettingList
     * @return void
     */
    @Override
    public void add(List<OrderSetting> orderSettingList) {
        if (orderSettingList != null && orderSettingList.size() > 0){
            for (OrderSetting orderSetting : orderSettingList) {
                //检查此数据(日期)是否存在
                long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
                if (count > 0){
                    //当前日期已经存在,执行更新
                    orderSettingDao.editNumberByOrderDate(orderSetting);
                }else {
                    //不存在，执行添加操作
                    orderSettingDao.add(orderSetting);
                }
            }
        }
    }

    /**
     * 按月查询预约数据
     * @author chenxys
     * @param date
     * @return java.util.List<java.util.Map>
     */
    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        //拼接起始日期 2022-5-1
        String begin = date + "-1";
        //拼接结束日期 2022-5-31
        String end = date + "-31";
        //封装参数
        Map<String,String> param = new HashMap<String,String>();
        param.put("begin",begin);
        param.put("end",end);
        //调用dao查询，根据日期范围查询预约设置数据
        List<OrderSetting> list = orderSettingDao.getOrderSettingByMonth(param);
        //存放处理后的数据
        List<Map> result = new ArrayList<Map>();
        //对数据进行处理
        if (list != null && list.size() > 0){
            //存放单条数据的map
            Map<String,Object> map = null;
            for (OrderSetting orderSetting : list) {
                map = new HashMap<String,Object>();
                //获取日期，几号
                int day = orderSetting.getOrderDate().getDate();
                map.put("date",day);
                //获取设置预约数
                int number = orderSetting.getNumber();
                map.put("number",number);
                //获取已经预约数
                int reservations = orderSetting.getReservations();
                map.put("reservations",reservations);
                //加入result集合
                result.add(map);
            }
        }
        return result;
    }

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        //查询当前日期是否被设置
        long countByOrderDate = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
        if(countByOrderDate > 0){
            //已经设置过，进行更新
            orderSettingDao.editNumberByOrderDate(orderSetting);
        }else {
            //未被设置，进行添加
            orderSettingDao.add(orderSetting);
        }
    }
}
